<?php
/**
 * Created by PHPStorm
 * User: xiest
 * Date: 2021/9/16
 * Email: <939369942@qq.com>
 */
namespace basic\listens;

use app\common\repositories\star\StarSendRecordRepository;
use basic\interfaces\ListenerInterface;
use Swoole\Timer;
use think\facade\Cache;
use think\facade\Log;
use think\swoole\Websocket;
use think\swoole\websocket\socketio\Handler;

class WebsocketConnectListen implements ListenerInterface
{
    public function handle($request): void
    {
        /**@var $wb Websocket**/
        $wb = app(Websocket::class);
        $sender = $wb->getSender();
        $token = input('token');
        // todo token获取uid
        if(!$token){
            $wb->push(json_encode([
                'type'=>'err_tip',
                'data'=>[
                    'msg'=>'登录状态失效！'
                ]
            ]));
            $wb->close();
        }
        $uid = $token;
        //保存用户的 uid 和 sender
        Cache::set('user_'.$sender.'_uid',$uid);
        Cache::set('user_send_'.$uid,$sender);


        Log::info('------------------数据'.input('token').'|'.$sender);
    }
}